package uk.ac.ebi.uniprot.wikipedia.NotCore;

import info.bliki.htmlcleaner.TagNode;
import info.bliki.wiki.filter.Encoder;
import info.bliki.wiki.filter.WikipediaParser;
import info.bliki.wiki.model.ImageFormat;
import info.bliki.wiki.model.WikiModel;
import info.bliki.wiki.tags.util.TagStack;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 06/03/12
 * Time: 13:27
 * To change this template use File | Settings | File Templates.
 */
public class UniProtWikiModel extends WikiModel {
    public UniProtWikiModel(String imageBaseURL, String linkBaseURL) {
        super(imageBaseURL, linkBaseURL);
    }
    public void parseInternalImageLink(String imageNamespace, String rawImageLink) {
        if (fExternalImageBaseURL != null) {
            String imageHref = fExternalWikiBaseURL;
            String imageSrc = fExternalImageBaseURL;
            ImageFormat imageFormat = ImageFormat.getImageFormat(rawImageLink, imageNamespace);

            String imageName = imageFormat.getFilename();
            /*String sizeStr = imageFormat.getWidthStr();
            if (sizeStr != null) {
                imageName = sizeStr + '-' + imageName;
            } */
            if (imageName.endsWith(".svg")) {
                imageName += ".png";
            }
            imageName = Encoder.encodeUrl(imageName);
            if (replaceColon()) {
                imageName = imageName.replace(':', '/');
            }
            String link = imageFormat.getLink();
            if (link != null) {
                if (link.length() == 0) {
                    imageHref = "";
                } else {
                    String encodedTitle = encodeTitleToUrl(link, true);
                    imageHref = imageHref.replace("${title}", encodedTitle);
                }

            } else {
                if (replaceColon()) {
                    imageHref = imageHref.replace("${title}", imageNamespace + '/' + imageName);
                } else {
                    imageHref = imageHref.replace("${title}", imageNamespace + ':' + imageName);
                }
            }
            imageSrc = imageSrc.replace("${image}", imageName);
            appendInternalImageLinkNoBorder(imageHref, imageSrc, imageFormat);
        }
    }
    public void appendInternalImageLinkNoBorder(String hrefImageLink, String srcImageLink, ImageFormat imageFormat) {
        String caption = imageFormat.getCaption();
        String imageType = imageFormat.getType();
        TagNode divInnerTagNode = new TagNode("div");
        divInnerTagNode.addAttribute("id", "image", false);
        if (hrefImageLink.length() != 0) {
            divInnerTagNode.addAttribute("href", hrefImageLink, false);
        }
        divInnerTagNode.addAttribute("src", srcImageLink, false);
        setDefaultThumbWidth(imageFormat);
        divInnerTagNode.addObjectAttribute("wikiobject", imageFormat);
        pushNode(divInnerTagNode);

        // TODO: test all these cases
        if (caption != null && caption.length() > 0
                && ("frame".equals(imageType) || "thumb".equals(imageType) || "thumbnail".equals(imageType))) {

            TagNode captionTagNode = new TagNode("div");
            String clazzValue = "caption";
            String type = imageFormat.getType();
            if (type != null) {
                clazzValue = type + clazzValue;
            }
            captionTagNode.addAttribute("class", clazzValue, false);
            //
            TagStack localStack = WikipediaParser.parseRecursive(caption, this, true, true);
            captionTagNode.addChildren(localStack.getNodeList());
            String altAttribute = imageFormat.getAlt();
            if (altAttribute == null) {
                altAttribute = captionTagNode.getBodyString();
                imageFormat.setAlt(Encoder.encodeHtml(altAttribute));// see issue #25
            }
            pushNode(captionTagNode);
            popNode();
        }
        popNode(); // div
    }
}
